/**
  @file: DomainHostGroup.java

   PURPOSE:
      This class represents an instance of a HostGroup in a Domain, either
      logical or physical.

   @author Ron Williams

   REVISION HISTORY ABSTRACT

   PROGRAMMER:  Ron Williams ORG: MSE

   Copyright 2011 Mission Solutions LLC. All rights reserved.

   END ABSTRACT
 */

package com.mse.utilities;

import java.lang.String;
import java.lang.StringBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import com.mse.utilities.DomainDefLimits;
import com.mse.utilities.DomainHost;
import com.mse.utilities.DomainException;

/**
   DESCRIPTION:
      The DomainHostGroup class contains the information as a result of parsing
      a Domain Definition file for a HostGroup definition.
 */
public class DomainHostGroup {

	/**
      DESCRIPTION:
         Create a DomainHostGroup object.
      @return void
	 */
	public DomainHostGroup() {
		this.gid       = "";
		this.groupName = "";
		this.hosts     = new HashMap<String, DomainHost>(20);
	}

	/**
      DESCRIPTION:
         Create a DomainHostGroup object based on specified attributes
      @param gid    - string containing the HostGroup's reference identifier
      @param name   - string specifying the HostGroup's name
      @return void
      @throws DomainException - for invalid arguments
	 */
	public DomainHostGroup(String gid, String name) throws DomainException {
		// verify input string sizes are not too long
		if (name.length() > DomainDefLimits.HG_GroupName_Max) {
			throw new DomainException("HostGroup, groupName field value is too " +
					"large, can not exceed length: " +
					DomainDefLimits.HG_GroupName_Max);
		}
		this.gid       = gid;
		this.groupName = name;
		this.hosts     = new HashMap<String, DomainHost>(20);
	}

	/**
      DESCRIPTION:
         get the hostgroup reference id.
      @return gid - this hostgroup reference ID
	 */
	public String getGID() {
		return gid;
	}

	/**
      DESCRIPTION:
         set the hostgroup reference id.
      @param gid - this hostgroup reference ID
	 */
	public void setGID(String gid) {
		this.gid = gid;
	}

	/**
      DESCRIPTION:
         get the hostgroup name
      @return name - this hostgroup name
	 */
	public String getName() {
		return groupName;
	}

	/**
      DESCRIPTION:
         set the hostgroup name
      @param name - this hostgroup name
	 */
	public void setName(String name) {
		this.groupName = name;
	}

	/**
      DESCRIPTION:
         add a DomainHost to this hostgroup. If the DomainHost is already in
         the group, a DomainException is thrown. The DomainHost "HID" determines
         uniqueness.

      @param host - a DomainHost to add to this hostgroup
      @throws DomainException
	 */
	public void addHost(DomainHost host) throws DomainException {
		String targetHID = host.getHID();

		if (hosts.containsKey(targetHID)) {
			throw new DomainException("HostGroup: failed to add host: " +
					targetHID + ", already in this Host Group");
		}

		hosts.put(targetHID, host);
	}

	/**
   DESCRIPTION:
      get all DomainHosts in this hostgroup.

   @return ArrayList - all DomainHosts in this hostgroup
	 */
	public Collection<DomainHost> getAllHosts() {
		return hosts.values();
	}

	/**
	   DESCRIPTION:
	      get host hashmap.

	   @return HashMap - containing all host of thehostgroup
		 */
		public HashMap<String, DomainHost> getAllHostsHM() {
			return hosts;
		}
		
	/**
      DESCRIPTION:
         get a DomainHost from this hostgroup that matches a host ID. If 
         DomainHost having the same Host ID is in the group, the DomainHost is
         returned. Otherwise null is returned. The returned DomainHost remains
         within this group.

      @param hid  - a DomainHost Reference Identifier to search for
	 */
	public DomainHost getHostByHostID(String hid) {
		return hosts.get(hid);
	}

	/**
      DESCRIPTION:
         determine if a host is in this hostgroup.
      @param host - we check if this host is in the group
      @return bool - true if the specified host is in the group, false otherwise
	 */
	public boolean hostInGroup(DomainHost host) {
		return hosts.containsKey(host.getHID());
	}

	/**
      DESCRIPTION:
         determine if a host is in this hostgroup.
      @param hid - we check if this host identifier is in the group
      @return bool - true if the specified host is in the group, false otherwise
	 */
	public boolean hostInGroup(String hid) {
		return hosts.containsKey(hid);
	}

	/**
      DESCRIPTION:
         obtain the number of DomainHosts in this hostgroup.
      @return num - integer number of hosts in this group
	 */
	public int numHostsInGroup() {
		return hosts.size();
	}


	/**
      DESCRIPTION:
         obtain the string representation for this host
      @return text - this host's info in a string for output
	 */
	public String toString() {
		StringBuilder sb = new StringBuilder(128);
		sb.append("Domain HostGroup - HGID: ");
		sb.append(gid);
		sb.append("\n          Group Name: ");
		sb.append(groupName);
		sb.append("\n         ");
		sb.append(hosts.size());
		sb.append(" Hosts in group, they are: ");
		for (DomainHost dh : hosts.values()) {
			sb.append("\n            ");
			sb.append(dh.getHID());
		}

		return sb.toString();
	}
	
	/**
	 * DESCRIPTION:
	 * 		removes the specified host from this host group
	 * @param hostId - HID of the host to remove
	 */
	public void removeHost(String hostId) {
		hosts.remove(hostId);
	}

	//---------------------------------------------------------------PRIVATE
	private String gid;              // HostGroup Reference Identifier
	private String groupName;        // display name for this HostGroup
	private HashMap<String, DomainHost> hosts;  // hosts in group
	// key=Host's HID
	

}

